package sf.database.jdbc.type;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

public class StringNVARCHARType implements TypeHandler<String> {
    public String get(ResultSet rs, String colName) throws SQLException {
        return rs.getNString(colName);
    }

    public String get(ResultSet rs, int index) throws SQLException {
        return rs.getNString(index);
    }

    @Override
    public String get(CallableStatement cs, int index) throws SQLException {
        return cs.getNString(index);
    }

    @Override
    public String get(CallableStatement cs, String parameterName) throws SQLException {
        return cs.getNString(parameterName);
    }

    @Override
    public void update(ResultSet rs, String columnLabel, Object value) throws SQLException {
        rs.updateNString(columnLabel, (String) value);
    }

    @Override
    public void update(ResultSet rs, int columnIndex, Object value) throws SQLException {
        rs.updateNString(columnIndex, (String) value);
    }

    public Object set(PreparedStatement stat, Object obj, int i) throws SQLException {
        if (null == obj) {
            stat.setNull(i, getSqlType());
        } else {
            stat.setNString(i, (String) obj);
        }
        return null;
    }

    @Override
    public Object set(CallableStatement cs, Object obj, String parameterName) throws SQLException {
        if (null == obj) {
            cs.setNull(parameterName, getSqlType());
        } else {
            cs.setNString(parameterName, (String) obj);
        }
        return null;
    }

    @Override
    public int getSqlType() {
        return Types.NVARCHAR;
    }

    @Override
    public Class<String> getDefaultJavaType() {
        return String.class;
    }
}
